package com.idcheck.utils;

import android.content.Context;
import android.os.Environment;
import android.os.storage.StorageManager;
import android.os.storage.StorageVolume;
import android.util.Log;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

import static android.content.Context.SEARCH_SERVICE;

public class FileUtil {
    private static final String TAG = "FileUtil";
    private static int countFile = 0;

    public static int getFileAndDirectory(File file) {
            if (file.isDirectory()) {
                File[] files = file.listFiles();
                for (File fileIndex : files) {
                    if (fileIndex.isDirectory()) {
                        getFileAndDirectory(fileIndex);
                    } else {
                        countFile++;
                    }
                }
            }
        //Log.d(TAG, "getFileAndDirectory: countFile = " + countFile);
        return countFile;
    }

    //该方法用于计算单层目录文件数量
    private static int mFileNum;
    public static int getFileNum(File file){
        if (file.isDirectory()){
            String[] strings = file.list();
            if (strings != null){
                mFileNum = strings.length;
            }
        }
        Log.d(TAG, "getFileNum: mFileNum = " + mFileNum);
        return mFileNum;
    }

    public static void saveDataToFile(byte[] bytes, float living ,int width, int height, int angle, String filePath){
        Log.d(TAG, "saveDataToFile: filePath = " + filePath);
        checkFolder(filePath);
        long currentTime = System.currentTimeMillis();
            File dataFile = new File(filePath + "/"+ "fake_" + living + "_" + width + "_" +  height + "_" + angle + "_" + currentTime + ".bitlog");
            if (!dataFile.exists()) {
                dataFile.getParentFile().mkdirs();
                try {
                    dataFile.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        if (bytes != null){
            Log.d(TAG, "living_test saveDataToFile: bytes.length = " + bytes.length);
        }
        saveByteToBit(bytes,dataFile);
    }


    private static void saveByteToBit(byte[] bytes, File filePath){
        try {
            FileOutputStream fos = new FileOutputStream(filePath);
            fos.write(bytes);
            fos.close();
            //Log.d(TAG, "living_test saveByteToBit: 写入成功: " + filePath);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void checkFolder(String path) {
        File folders = new File(path);
        if (!folders.exists()){
            folders.mkdirs();
        }
    }
    public static void scanFileList(File dir, List<String> fileNames) {
        if (!dir.exists() || !dir.isDirectory()) {// 判断是否存在目录
            return;
        }
        String[] files = dir.list();// 读取目录下的所有目录文件信息
        for (int i = 0; i < files.length; i++) {// 循环，添加文件名或回调自身
            File file = new File(dir, files[i]);
            if (file.isFile()) {// 如果文件
                fileNames.add(dir + File.separator+ file.getName());// 添加文件全路径名
            } else {// 如果是目录
                scanFileList(file, fileNames);// 回调自身继续查询
            }
        }
    }
    public static boolean isExternalStorage(){
        String state=Environment.getExternalStorageState();
        if(state.equals(Environment.MEDIA_MOUNTED)){
            return true;
        }else{
            return false;
        }
    }
    public static String[] getAllSdPaths(Context mContext) {
        String path1 = "";// 内部存储路径
        String path2 = "";// sd卡存储路径
        String path3 = "";// usb存储路径
        StorageManager mStorageManager = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE);
        Class<?> storageVolumeClazz = null;
        try {
            storageVolumeClazz = Class.forName("android.os.storage.StorageVolume");
            Method getVolumeList = mStorageManager.getClass().getMethod("getVolumeList");
            Method getPath = storageVolumeClazz.getMethod("getPath");
            Method isRemovable = storageVolumeClazz.getMethod("isRemovable");
            Method getDescription = storageVolumeClazz.getMethod("getDescription", Context.class);
            Object result = getVolumeList.invoke(mStorageManager);
            final int length = Array.getLength(result);
            for (int i = 0; i < length; i++) {
                Object storageVolumeElement = Array.get(result, i);
                String path = (String) getPath.invoke(storageVolumeElement);
                String description = (String) getDescription.invoke(storageVolumeElement, mContext);
                boolean removable = (Boolean) isRemovable.invoke(storageVolumeElement);
                if (true == removable) {// 可拆卸设备
                    if (description.endsWith("USB") || description.endsWith("U 盘")||description.endsWith("USB 存储器") ) {// usb外置卡
                        path3 = path;
                    }else if(description.endsWith("SD")||description.endsWith("SD 卡")){//sd卡可判断
                        path2=path;
                    }else{//其它sd卡不可通过SD、SD卡来判断识别
                        path2=path;
                    }
                } else {// 内置卡存储路径
                    path1 = path;
                }
            }
            String[] pathLists = new String[] { path1, path2, path3 };
            return pathLists;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }


}
